package com.huaweicloud.apm.example;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.annotation.JsonProperty;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class ListBusiness {

    private static final String IAM_HEADER_TOEKN = "X-Auth-Token";

    private static final String IAM_TOKEN_VALUE = "{用户获取的token}";

    private static final String OPEN_API_URL_AK_SK_LIST
        = "https://{endPoint}/v1/apm2/openapi/cmdb/business/get-business-list";

    public static GetBusinessListVO getBusinessListVO() {
        HttpHeaders headers = new HttpHeaders();
        headers.add(IAM_HEADER_TOEKN, IAM_TOKEN_VALUE);

        RestTemplate restTemplate = Utils.restTemplate();
        ResponseEntity<String> response = restTemplate.exchange(OPEN_API_URL_AK_SK_LIST, HttpMethod.GET,
            new HttpEntity<String>(headers), String.class, new HashMap<>());
        String body = response.getBody();
        JSONObject jsonObject = JSON.parseObject(body, JSONObject.class);
        GetBusinessListVO vo = new GetBusinessListVO();
        List<BusinessNodeModel> nodeModels = new ArrayList<>();
        JSONArray jsonArray = jsonObject.getJSONArray("business_nodes");
        for (int i = 0; i < jsonArray.size(); i++) {
            JSONObject json = jsonArray.getJSONObject(i);
            BusinessNodeModel businessNodeModel = new BusinessNodeModel();
            businessNodeModel.setId(json.getLong("id"));
            businessNodeModel.setDisplayName(json.getString("display_name"));
            businessNodeModel.setEpsId(json.getString("eps_id"));
            businessNodeModel.setGmtModify(json.getDate("gmt_modify"));
            businessNodeModel.setInnerDomainId(json.getInteger("inner_domain_id"));
            businessNodeModel.setName(json.getString("name"));
            businessNodeModel.setDefault(json.getBoolean("is_default"));
            businessNodeModel.setGmtCreate(json.getDate("gmt_create"));
            nodeModels.add(businessNodeModel);
        }
        vo.setBusinessNodes(nodeModels);
        return vo;
    }

    static class GetBusinessListVO {
        @JsonProperty("business_nodes")
        private List<BusinessNodeModel> businessNodes;

        public GetBusinessListVO() {

        }

        public List<BusinessNodeModel> getBusinessNodes() {
            return businessNodes;
        }

        public void setBusinessNodes(List<BusinessNodeModel> businessNodes) {
            this.businessNodes = businessNodes;
        }
    }

    static class BusinessNodeModel implements Serializable {
        private static final long serialVersionUID = 1L;

        @JsonProperty("name")
        private String name;

        @JsonProperty("gmt_create")
        private Date gmtCreate;

        @JsonProperty("display_name")
        private String displayName;

        @JsonProperty("inner_domain_id")
        private Integer innerDomainId;

        @JsonProperty("is_default")
        private Boolean isDefault;

        @JsonProperty("id")
        private Long id;

        @JsonProperty("gmt_modify")
        private Date gmtModify;

        @JsonProperty("eps_id")
        private String epsId;

        public String getName() {
            return name;
        }

        public String getEpsId() {
            return epsId;
        }

        public Integer getInnerDomainId() {
            return innerDomainId;
        }

        public void setDisplayName(String displayName) {
            this.displayName = displayName;
        }

        public Boolean getDefault() {
            return isDefault;
        }

        public Date getGmtModify() {
            return gmtModify;
        }

        public void setDefault(Boolean aDefault) {
            isDefault = aDefault;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setInnerDomainId(Integer innerDomainId) {
            this.innerDomainId = innerDomainId;
        }

        public void setEpsId(String epsId) {
            this.epsId = epsId;
        }

        public Long getId() {
            return id;
        }

        public String getDisplayName() {
            return displayName;
        }

        public Date getGmtCreate() {
            return gmtCreate;
        }

        public void setGmtCreate(Date gmtCreate) {
            this.gmtCreate = gmtCreate;
        }

        public void setGmtModify(Date gmtModify) {
            this.gmtModify = gmtModify;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public BusinessNodeModel() {
        }
    }

}
